{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.normal(0,1,size=10000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0.38846098, -1.63157588, -0.88842635, ...,  0.46624752,\n        1.00052948, -1.00351278])"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-5.212204538783405"
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "np.min(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "8817926"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "np.argmin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-0.01169414814604257"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "x[9733915]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5685928"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "np.argmax(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-0.7608032261768024"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "x[1731152]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序和使用索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.arange(16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 5, 15,  1, 14,  8,  3,  6,  4,  7, 10,  9,  0,  2, 12, 11, 13])"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "np.sort(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 5, 15,  1, 14,  8,  3,  6,  4,  7, 10,  9,  0,  2, 12, 11, 13])"
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "x.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.random.randint(10, size=(4,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 5, 8, 0],\n       [6, 6, 1, 4],\n       [5, 2, 5, 3],\n       [5, 9, 8, 2]])"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 0, 5, 8],\n       [1, 4, 6, 6],\n       [2, 3, 5, 5],\n       [2, 5, 8, 9]])"
     },
     "metadata": {},
     "execution_count": 44
    }
   ],
   "source": [
    "np.sort(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 0, 5, 8],\n       [1, 4, 6, 6],\n       [2, 3, 5, 5],\n       [2, 5, 8, 9]])"
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "source": [
    "np.sort(X, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 2, 1, 0],\n       [5, 5, 5, 2],\n       [5, 6, 8, 3],\n       [6, 9, 8, 4]])"
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "source": [
    "np.sort(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 47
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 5, 11,  9,  7,  2,  8,  4,  6, 13,  1, 12,  3, 15,  0, 14, 10])"
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([13,  9,  4, 11,  6,  0,  7,  3,  5,  2, 15,  1, 10,  8, 14, 12],\n      dtype=int64)"
     },
     "metadata": {},
     "execution_count": 50
    }
   ],
   "source": [
    "np.argsort(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6, 11, 13,  8, 12,  7, 15,  9, 14, 10])"
     },
     "metadata": {},
     "execution_count": 51
    }
   ],
   "source": [
    "np.partition(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([13,  9,  4, 11,  6,  0,  7,  1,  8,  5, 10,  3, 12,  2, 14, 15],\n      dtype=int64)"
     },
     "metadata": {},
     "execution_count": 52
    }
   ],
   "source": [
    "np.argpartition(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 5, 8, 0],\n       [6, 6, 1, 4],\n       [5, 2, 5, 3],\n       [5, 9, 8, 2]])"
     },
     "metadata": {},
     "execution_count": 53
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 3, 1, 2],\n       [2, 3, 0, 1],\n       [1, 3, 0, 2],\n       [3, 0, 2, 1]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 54
    }
   ],
   "source": [
    "np.argsort(X, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 2, 1, 0],\n       [2, 0, 2, 3],\n       [3, 1, 0, 2],\n       [1, 3, 3, 1]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 55
    }
   ],
   "source": [
    "np.argsort(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 3, 1, 2],\n       [2, 3, 0, 1],\n       [1, 3, 2, 0],\n       [3, 0, 2, 1]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 56
    }
   ],
   "source": [
    "np.argpartition(X, 2, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 2, 1, 0],\n       [2, 0, 2, 3],\n       [3, 1, 0, 2],\n       [1, 3, 3, 1]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 57
    }
   ],
   "source": [
    "np.argpartition(X, 2, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}